﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using System.IO;
namespace Com.CMS263.Web.manage.ProductManage
{
    public partial class product_img_edit : System.Web.UI.Page
    {
        ProductImagesBLL pbll = new ProductImagesBLL();
        public int ProductId;
        public int id;
        public ProductImage ab = new ProductImage();
        protected void Page_Load(object sender, EventArgs e)
        {
            ProductId = Convert.ToInt32(Request["ProductId"]);
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string title = StringUtil.checkStr(Request["txtTitle"]);

            HttpPostedFile file = Request.Files["txtProductImg"];
            if (string.IsNullOrEmpty(title))
            {
                StringUtil.Alert("名称不能为空", "-1");
            }


            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            ProductImage prod = new ProductImage();
            id = Convert.ToInt32(Request["id"]);
            prod = (ProductImage)pbll.FindModelById("ProductImage", id);
            if (file.ContentLength >0)
            {
                prod.FilePath = strDay + "/" + Util.UpLoadFileUtil.UpLoadFile(file, savePath);
            }
            prod.Title = title;
            pbll.entity = prod;
            if (pbll.UpDate())
            {
                StringUtil.Alert("修改成功", "product_img_list.aspx?ProductId=" + ProductId);
            }


        }
        public void BindData()
        {
            int id = Convert.ToInt32(Request["id"]);
            if (id <= 0)
            {
                StringUtil.Alert("抱歉，没有找到相关数据", "product_img_list.aspx?ProductId=" + ProductId);
            }
            ab = (ProductImage)pbll.FindModelById("ProductImage", id);
        }

        Product pp = new Product();
        ProductBLL prodbll = new ProductBLL();
        public Product GetProduct(object obj)
        {
            int ProductId = Convert.ToInt32(obj);
            pp = (Product)prodbll.FindModelById("Product", ProductId);
            if (pp == null || pp.Id < 1)
            {
                pp = new Product();
            }
            return pp;
        }

    }
}
